# Makefile for OTA Client

# 构建信息
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S')
LDFLAGS := -X main.version=$(VERSION) -X main.buildTime=$(BUILD_TIME)

# 默认目标
.PHONY: all build clean run test deps

all: build

# 构建
build:
	@echo "构建 OTA Client..."
	go build -ldflags "$(LDFLAGS)" -o bin/ota-client .

# 构建所有平台
build-all:
	@echo "构建多平台版本..."
	@mkdir -p bin
	GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o bin/ota-client-linux-amd64 .
	GOOS=linux GOARCH=arm64 go build -ldflags "$(LDFLAGS)" -o bin/ota-client-linux-arm64 .
	GOOS=darwin GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o bin/ota-client-darwin-amd64 .
	GOOS=darwin GOARCH=arm64 go build -ldflags "$(LDFLAGS)" -o bin/ota-client-darwin-arm64 .
	GOOS=windows GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o bin/ota-client-windows-amd64.exe .

# 安装依赖
deps:
	@echo "安装依赖..."
	go mod download
	go mod tidy

# 运行
run: build
	./bin/ota-client -f config.yaml

# 运行（使用自定义配置）
run-config:
	./bin/ota-client -f $(CONFIG)

# 测试
test:
	go test -v ./...

# 清理
clean:
	@echo "清理构建文件..."
	rm -rf bin/
	rm -rf temp/

# 安装
install: build
	@echo "安装 OTA Client..."
	cp bin/ota-client /usr/local/bin/

# 创建配置文件模板
config-template:
	@echo "创建配置文件模板..."
	cp config.yaml config-template.yaml

# 帮助
help:
	@echo "可用命令:"
	@echo "  build         - 构建程序"
	@echo "  build-all     - 构建所有平台版本"
	@echo "  deps          - 安装依赖"
	@echo "  run           - 运行程序"
	@echo "  run-config    - 使用自定义配置运行 (需要设置 CONFIG 变量)"
	@echo "  test          - 运行测试"
	@echo "  clean         - 清理构建文件"
	@echo "  install       - 安装到系统"
	@echo "  config-template - 创建配置文件模板"
	@echo "  help          - 显示帮助信息" 